package com.example.experiment3.service;

import com.example.experiment3.model.RegionStats;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DataService implements InitializingBean {

    private static final String Data_URL="https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
    private final static List<RegionStats> allRegionStats = new ArrayList<>();
    public static List<RegionStats> getAllRegionStats(){
        return allRegionStats;
    }
    @Scheduled(cron = "0 0 * * * ?")  //每一个小时执行一次
    public List<RegionStats> fetchCoronaVirusData() throws IOException {

        RequestEntity<Void> requestEntity = RequestEntity
                .get(URI.create(Data_URL))
                .headers(httpHeaders -> httpHeaders.add( "User-Agent","WM"))
                .build();
        ResponseEntity<Resource> exchange = new RestTemplate().exchange(requestEntity,Resource.class);
        Resource body = exchange.getBody();

        InputStream ii=body.getInputStream();
        BufferedInputStream in=null;
        BufferedOutputStream out=null;
        in=new BufferedInputStream(ii);
        out=new BufferedOutputStream(new FileOutputStream("C:\\Users\\浅栀\\Desktop\\练手\\j2e\\大三下\\experiment3\\src\\main\\resources\\static\\1.csv"));
        int len=-1;
        byte[] b=new byte[1024];
        while((len=in.read(b))!=-1){
            out.write(b,0,len);
        }
        in.close();
        out.close();

        String inpath="C:\\Users\\浅栀\\Desktop\\练手\\j2e\\大三下\\experiment3\\src\\main\\resources\\static\\1.csv";
        List<RegionStats> list = new ArrayList<RegionStats>();
        BufferedReader reader = new BufferedReader(new FileReader(inpath)); // 读取CSV文件
        String line = null;
        line=reader.readLine();
        line=reader.readLine();
        while (line!= null)
        {
            String[] row = line.split("[,]");
            int i = row.length-1;
            int j = row.length-2;
            int h=0;
            RegionStats infos = new RegionStats();
            infos.setStats(row[0]);
            infos.setCountry(row[1]);
            row[0]="1";
            row[1]="1";
            row[2]="1";
            row[3]="1";
            row[4]="1";
            int[] array = Arrays.stream(row).mapToInt(Integer::parseInt).toArray();
            infos.setLastcases(array[i]);
            array[h]=array[i]-array[j];
            infos.setDifferentday(array[h]);
            list.add(infos);
            line = reader.readLine();
        }

            return list;

    }


    @Override
    public void afterPropertiesSet() throws Exception {
            fetchCoronaVirusData();
    }

}
